package com.besovideo.mpu;

import org.opencv.facetrackmatch.facedetect.FaceTrackTool;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class FaceDetectRateDialog extends Dialog {

	Context context;
	FaceTrackTool faceTrackTool;
	int lay;
	RadioGroup m_RadioGroup;
	RadioButton m_RadioButtonFaceDetectRateOne;
	RadioButton m_RadioButtonFaceDetectRateTwo;
	RadioButton m_RadioButtonFaceDetectRateFour;
	RadioButton m_RadioButtonFaceDetectRateEight;
	RadioButton m_RadioButtonFaceDetectRateMax;

	private static final int RATE_ONE = 1;
	private static final int RATE_TWO = 2;
	private static final int RATE_FOUR = 4;
	private static final int RATE_EIGHT = 8;
	private static final int RATE_MAX = 16;

	public FaceDetectRateDialog(Context context, FaceTrackTool faceTrackTool) {
		super(context, R.style.TransparentDialogHaveTitle);
		this.context = context;
		this.faceTrackTool = faceTrackTool;
		this.lay = R.layout.face_detect_rate;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(lay);

		m_RadioGroup = (RadioGroup) findViewById(R.id.radioGroupFaceDetectRate);
		m_RadioButtonFaceDetectRateOne = (RadioButton) findViewById(R.id.radioButtonFaceDetectRateOne);
		m_RadioButtonFaceDetectRateTwo = (RadioButton) findViewById(R.id.radioButtonFaceDetectRateTwo);
		m_RadioButtonFaceDetectRateFour = (RadioButton) findViewById(R.id.radioButtonFaceDetectRateFour);
		m_RadioButtonFaceDetectRateEight = (RadioButton) findViewById(R.id.radioButtonFaceDetectRateEight);
		m_RadioButtonFaceDetectRateMax = (RadioButton) findViewById(R.id.radioButtonFaceDetectRateMax);

		SelectSet();
		m_RadioGroup.setOnCheckedChangeListener(new RadioGroupCheckChange());
	}

	private void SelectSet() {
		switch (faceTrackTool.getmDetectRate()) {
		case RATE_ONE:
			m_RadioGroup.check(m_RadioButtonFaceDetectRateOne.getId());
			break;
		case RATE_TWO:
			m_RadioGroup.check(m_RadioButtonFaceDetectRateTwo.getId());
			break;
		case RATE_FOUR:
			m_RadioGroup.check(m_RadioButtonFaceDetectRateFour.getId());
			break;
		case RATE_EIGHT:
			m_RadioGroup.check(m_RadioButtonFaceDetectRateEight.getId());
			break;
		case RATE_MAX:
			m_RadioGroup.check(m_RadioButtonFaceDetectRateMax.getId());
			break;

		default:
			break;
		}
	}

	private class RadioGroupCheckChange implements OnCheckedChangeListener {

		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			MainActivity parent = (MainActivity) context;
			if (checkedId == m_RadioButtonFaceDetectRateOne.getId()) {
				parent.subConfigMenuListViewAdapter.setText(
						MainActivity.SubConfigMenuEnum.SUB_MENU_DETECTRATE
								.value(), m_RadioButtonFaceDetectRateOne
								.getText().toString());
				faceTrackTool.setmDetectRate(RATE_ONE);
			} else if (checkedId == m_RadioButtonFaceDetectRateTwo.getId()) {
				parent.subConfigMenuListViewAdapter.setText(
						MainActivity.SubConfigMenuEnum.SUB_MENU_DETECTRATE
								.value(), m_RadioButtonFaceDetectRateTwo
								.getText().toString());
				faceTrackTool.setmDetectRate(RATE_TWO);
			} else if (checkedId == m_RadioButtonFaceDetectRateFour.getId()) {
				parent.subConfigMenuListViewAdapter.setText(
						MainActivity.SubConfigMenuEnum.SUB_MENU_DETECTRATE
								.value(), m_RadioButtonFaceDetectRateFour
								.getText().toString());
				faceTrackTool.setmDetectRate(RATE_FOUR);
			} else if (checkedId == m_RadioButtonFaceDetectRateEight.getId()) {
				parent.subConfigMenuListViewAdapter.setText(
						MainActivity.SubConfigMenuEnum.SUB_MENU_DETECTRATE
								.value(), m_RadioButtonFaceDetectRateEight
								.getText().toString());
				faceTrackTool.setmDetectRate(RATE_EIGHT);
			} else {
				parent.subConfigMenuListViewAdapter.setText(
						MainActivity.SubConfigMenuEnum.SUB_MENU_DETECTRATE
								.value(), m_RadioButtonFaceDetectRateMax
								.getText().toString());
				faceTrackTool.setmDetectRate(RATE_MAX);
			}
			parent.subConfigMenuListViewAdapter.notifyDataSetChanged();
			dismiss();
		}
	}
}
